<template>
    <div>
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
            <el-form-item label="飞机编号" prop="plane_id">
                <el-input v-model="form.plane_id"></el-input>
            </el-form-item>
            <el-form-item label="航行城市">
                <el-col :span="11">
                    <el-input placeholder="输入出发城市" v-model="form.from_city" style="width: 100%;"></el-input>
                </el-col>
                <el-col class="line" :span="1" :offset="1">-</el-col>
                <el-col :span="11">
                    <el-input placeholder="输入到达城市" v-model="form.to_city" style="width: 100%;"></el-input>
                </el-col>
            </el-form-item>
            <el-form-item label="出发时间" prop="startTime">
                <el-date-picker type="datetime" placeholder="选择出发时间" v-model="form.startTime" style="width: 100%;"></el-date-picker>
            </el-form-item>
            <el-form-item label="到达时间" prop="arriveTime">
                <el-date-picker type="datetime" placeholder="选择到达时间" v-model="form.arriveTime" style="width: 100%;"></el-date-picker>
            </el-form-item>
            <el-form-item label="延误" v-if="disable">
                <el-switch v-model="isDelay"></el-switch>
            </el-form-item>
            <el-form-item label="经济舱价格" prop="touristClassPrice">
                <el-input v-model="form.touristClassPrice"></el-input>
            </el-form-item>
            <el-form-item label="商务舱价格" prop="businessClassPrice">
                <el-input v-model="form.businessClassPrice"></el-input>
            </el-form-item>
            <el-form-item label="头等舱价格" prop="firstClassPrice">
                <el-input v-model="form.firstClassPrice"></el-input>
            </el-form-item>
            <el-form-item label="备注">
                <el-input type="textarea" v-model="form.remark"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="addFlight" v-if="!disable">立即创建</el-button>
                <el-button type="primary" @click="saveFlight" v-else>保存信息</el-button>
                <el-button>取消</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
export default {
    name:'plane-editor',
    props:{
        form:{
            type:Object
        },
        disable:{
            type:Boolean
        }
    },
    data(){
        return{
            rules:{
                plane_id:[
                    { required: true, message: '请输入飞机编号', trigger: 'blur' }
                ],
                startTime:[
                    { required: true, message: '请输入出发时间', trigger: 'blur' }
                ],
                arriveTime:[
                    { required: true, message: '请输入到达时间', trigger: 'blur' }
                ]
            },
            isDelay:false,
            fare:{}
        }
    },
    methods:{
        addFlight(){
            let item = this.form;
            if (this.checkEmpty(item.plane_id, "飞机编号") || this.checkEmpty(item.from_city, "出发城市") ||
                this.checkEmpty(item.to_city, "到达城市") || this.checkEmpty(item.startTime, "出发时间") || 
                this.checkEmpty(item.arriveTime, "到达时间") || this.checkPrice()) {
                return;
            }
            item.admin_id = this.$cookies.get("admin").admin_id;
            this.$http.post("/api/flight/addFlight",item).then((res)=>{
                console.log(res);
                this.showMessage("添加成功","success");
                this.$emit('closeDialog');
                this.$emit('refresh');
            })
        },
        saveFlight(){
            this.form.state = this.isDelay? 1:0;
            let item = this.form;
            if (this.checkEmpty(item.plane_id, "飞机编号") || this.checkEmpty(item.from_city, "出发城市") ||
                this.checkEmpty(item.to_city, "到达城市") || this.checkEmpty(item.startTime, "出发时间") || 
                this.checkEmpty(item.arriveTime, "到达时间") || this.checkPrice()) {
                return;
            }
            item.admin_id = this.$cookies.get("admin").admin_id;
            console.log(item);
            this.$http.post("/api/flight/editFlight",item).then((res)=>{
                console.log(res);
                this.showMessage("修改成功","success");
                this.$emit('closeDialog');
            })
        },
        getFare(){
            this.$http.get("/api/fare/getFare").then((res)=>{
                console.log(res);
                this.fare = res.data
            })
        },
        checkPrice(){
            let item = this.form;
            let fare = this.fare;
            if(this.comparePrice(item.touristClassPrice,fare.touristClassPrice,fare,"经济舱")
            ||this.comparePrice(item.businessClassPrice,fare.businessClassPrice,fare,"商务舱")
            ||this.comparePrice(item.firstClassPrice,fare.firstClassPrice,fare,"头等舱")){
                return true
            }
            return false;
        },
        comparePrice(price,fare,range,msg){
            let top = fare * (1+range.floatingRange)
            let bottom = fare * (1-range.decreaseRange)
            if(price > top){
                this.showMessage(`${msg}价格高于上浮标准,最高价格为${top}`,'error');
                return true
            }else if(price < bottom){
                this.showMessage(`${msg}价格低于下浮标准,最低价格为${bottom}`,'error');
                return true
            }else{
                return false;
            }
        }
    },
    mounted(){
        this.getFare()
    }
}
</script>